package com.xy.modular.owner.controller;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.pager.SearchCondition;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.owner.model.OwnerHouse;
import com.xy.modular.owner.service.IOwnerHouseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * Created by BCK on 2019/09/14.
 */
@Controller
@RequestMapping("/wx/ownerHouse/")
public class OwnerHouseController extends BaseController {

	@Resource
	IOwnerHouseService ownerHouseService;

	@RequestMapping("/save")
	@ResponseBody
	public MReturnObject add(OwnerHouse ownerHouse) {
		OwnerHouse oh = ownerHouseService.getByOwnerId(ownerHouse.getAuthId());

		if (oh != null) {
			oh.setHouseId(ownerHouse.getHouseId());
			ownerHouseService.update(oh);
			ownerHouse = oh;
		} else {
			ownerHouse.setId(UUIDUtils.UUID());
			ownerHouseService.save(ownerHouse);
		}
		return new MReturnObject(MReturnObject.SUCCESS, ownerHouseService.getById(ownerHouse.getId()));
	}

	@RequestMapping("/by/{owenrId}")
	@ResponseBody
	public MReturnObject list(OwnerHouse ownerHouse, HttpServletRequest request, @PathVariable String owenrId) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.setColumns(" basics_owner_house.*,h.name ");
		pageRequest.addAndCondition(OwnerHouse.AUTHID, SearchCondition.EQUAL, owenrId);
		pageRequest.setLeftJoinSql(" left join  basics_house h on h.id=basics_owner_house.house_id ");
		return new MReturnObject(MReturnObject.SUCCESS, ownerHouseService.findColumnsAll(pageRequest));
	}
}
